<?php
namespace app\api\controller\v2\model\dynamic;

use think\Model;
use think\Db;

class Dynamic extends model
{
    //个人动态,显示图片
    public function getShowDynamicImg($user_id,$pageno){
        $pagesize = 12;
        $startno = $pageno *$pagesize;

        $result = Db::query("select dynamic_id,user_id from ym_user_dynamic where user_id = '$user_id' ORDER BY send_time desc limit {$startno},{$pagesize}");
        $count = count($result);
        for ($i=0; $i < $count; $i++) {
            $dynamic_id = $result[$i]['dynamic_id'];
            $img_url = Db::query("select img_url from ym_user_dynamic_img where dynamic_id = '$dynamic_id' limit 1");
            $result[$i]['img_url'] = $img_url[0]['img_url'];
        }

        return $result;
    }

//显示个人动态评论
    public function get_show_dynamic_cmmt($user_id,$dynamic_id,$pageno){
        $pagesize = 10;
        $startno = $pageno *$pagesize;
        $result = Db::query("select a.cmmt_id,a.cmmt_content,a.create_time,b.user_id,b.user_name,b.small_user_icon as user_icon,c.cmmt_upvoteCount 
                              from ym_user_dynamic_cmmt as a 
                              join user_info as b on a.user_id = b.user_id 
                              join ym_user_dynamic_cmmt_upvoteCount as c on a.cmmt_id = c.cmmt_id 
                              where a.dynamic_id = '$dynamic_id' 
                              ORDER BY c.cmmt_upvoteCount 
                              desc,a.create_time 
                              desc limit {$startno} , {$pagesize}");
        $length = count($result);
        for ($i=0; $i <$length ; $i++) {
            $cmmt_id = $result[$i]['cmmt_id'];
            $count = Db::query("select count(*) as count from ym_user_dynamic_cmmt where dynamic_id = '$dynamic_id'");
            $result[$i]['cmmt_count'] = $count[0]['count'];
            $super = Db::query("select super_user_id from ym_user_dynamic_cmmt where cmmt_id = '$cmmt_id'");
            $super_user_id = $super[0]['super_user_id'];
            if (!is_null($super_user_id)) {
                $user = Db::query("select user_id as super_user_id,user_name from user_info where user_id = '$super_user_id'");
                $result[$i]['super_user_id'] = $user[0]['super_user_id'];
                $result[$i]['super_user_name'] = $user[0]['user_name'];
            }else{
                $result[$i]['super_user_id'] = null;
                $result[$i]['super_user_name'] = null;
            }
            $upvote = Db::query("select cmmt_upvote from ym_user_dynamic_cmmt_upvote where cmmt_id = '$cmmt_id' and user_id = '$user_id'");
            // echo $upvote[0]['cmm_upvote'];
            if (is_null($upvote[0]['cmmt_upvote'])) {
                $result[$i]['cmmt_upvote'] = 0;
            }else{
                $result[$i]['cmmt_upvote'] = $upvote[0]['cmmt_upvote'];
            }
        }
        return $result;
    }


//显示用户个人动态
    public function getShowUserDynamic($dynamic_id,$user_id){

        $dynamic = Db::query("select b.user_id,b.user_name,b.user_icon,a.dynamic_id,a.content,a.latitude as post_latitude,a.longitude as post_longitude,a.user_address,a.send_time,c.upvoteCount from ym_user_dynamic as a left join user_info as b on a.user_id=b.user_id join ym_user_dynamic_upvoteCount as c on a.dynamic_id = c.dynamic_id where a.dynamic_id = '$dynamic_id' ");

        $length = count($dynamic);
        for ($i=0; $i < $length; $i++) {
            $dynamic_id = $dynamic[$i]['dynamic_id'];
            $img = Db::query("select img_url from ym_user_dynamic_img where dynamic_id = '$dynamic_id'");
            $dynamic[$i]['img_urls'] = $img;
            $count = Db::query("select count(*) as count from ym_user_dynamic_cmmt where dynamic_id = '$dynamic_id'");
            $dynamic[$i]['cmmt_count'] = $count[0]['count'];
            $upvote = Db::query("select upvote from ym_user_dynamic_upvote where dynamic_id = '$dynamic_id' and user_id = '$user_id'");
            if (is_null($upvote[0]['upvote'])) {
                $dynamic[$i]['upvote'] = 0;
            }else{
                $dynamic[$i]['upvote'] = $upvote[0]['upvote'];
            }
        }
        return $dynamic;
    }

    /**
     * 获取动态
     * @param $user_id
     * @param $row
     * @param $count
     * @return array|bool
     */
    public function getShowUserDynamicPading($user_id, $row, $count)
    {
        //按时间排序，获取用户的正文，图片，时间，取$count条
        try{
            $data = [];
            //先获取动态id
            $dynamics = Db::name('ym_user_dynamic')->where('user_id',$user_id)->field('dynamic_id,content,send_time')->order('send_time desc')->limit($row,$count)->select();
            foreach($dynamics as $dynamic)
            {
                $id = $dynamic['dynamic_id'];
                //查找对应动态的图片
                $imgs = Db::name('ym_user_dynamic_img')->where('dynamic_id',$id)->column('img_url');
                $dynamic['imgs'] = $imgs;
                $data[] = $dynamic;
            }
            return ['status'=>true,'data'=>$data];
        }catch(\Exception $e){
            echo $e->getMessage();
            return ['status'=>false];
        }
    }









}
